package Classes.GameViews.ViewImgs// ActionScript file
{
	
	import Classes.GameErrors.GameError;
	import Classes.Managers.M_LoadCartoon;
	
	import MV_BMP.BMPMCEvent;
	import MV_BMP.MovieClip_BMP;
	
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 *动画显示对象类 
	 * @author 葛增文
	 * 
	 */	
	public class CartoonViewImg extends Sprite
	{
		private var _imgCartoonsObj:Object;/*显示动画存储器*/
		private var _nowPlayingCartoonStr:String;/*当前播放动画的名字*/
		private var _baseName:String;/*基本动画名字(动画资源名)*/
		private var _cartoonLayer:Sprite;/*卡通放置层*/
		public function CartoonViewImg(baseName:String):void
		{
			this._baseName=baseName;
			this.init();
			this.getCartoons(this.getMyTmpSourceName(),this.getMyCartoonsStrAry());
		}
		
		private function init():void
		{
			this._imgCartoonsObj=new Object;
			this._cartoonLayer=new Sprite;
			this.addChild(this.cartoonLayer);
		}
		
		private function addListener():void
		{
			this.addEventListener(BMPMCEvent.LAST_FRAME,lastFrameHandle);
			this.addEventListener(BMPMCEvent.LABEL_CHANGE,labelChangeHandle);
		}
		
		private function removeListener():void
		{
			this.removeEventListener(BMPMCEvent.LAST_FRAME,lastFrameHandle);
			this.removeEventListener(BMPMCEvent.LABEL_CHANGE,labelChangeHandle);
		}
		
		/**
		 *动画进入最后一帧 
		 * @param e
		 * 
		 */		
		private function lastFrameHandle(e:BMPMCEvent):void
		{
			this.dispatchEvent(new Event("cartoonPlayOver"));
		}
		
		/**
		 *帧标签改变 
		 * @param e
		 * 
		 */		
		private function labelChangeHandle(e:BMPMCEvent):void
		{
			
		}
		
		/**
		 *获得动画 
		 * @param tmpSourceName 临时资源名
		 * @param cartoonsStrAry 动画名数组 
		 * 
		 */		
		public function getCartoons(tmpSourceName:String,cartoonsStrAry:Array):void
		{
			var sourceName:String;
			if(M_LoadCartoon.instance.sourceCheck(this.baseName,this))
			{
				sourceName=this.baseName;
			}
			else
			{
				sourceName=tmpSourceName;
			}
			for each(var cartoonStr:String in cartoonsStrAry)
			{
				this.imgCartoonsObj[cartoonStr]=M_LoadCartoon.instance.getCartoon(sourceName,sourceName+"_"+cartoonStr);
			}
		}
		
		/**
		 *需求资源载入结束 
		 * 
		 */		
		public function sourceLoadOver():void
		{
			for(var k:String in this.imgCartoonsObj)
			{
				this.imgCartoonsObj[k]=M_LoadCartoon.instance.getCartoon(this.baseName,this.baseName+"_"+k);
				if(k==this.nowPlayingCartoonStr)
				{
					var nowCartoon:MovieClip_BMP=this.cartoonLayer.getChildAt(0) as MovieClip_BMP;
					var isStop:Boolean=nowCartoon.isStop;
					var nowFrame:int=nowCartoon.currentFrame;
					this.playCartoon(this.nowPlayingCartoonStr,isStop,nowFrame);
				}
			}
		}
		
		/**
		 *播放动画 
		 * @param cartoonStr 动画名
		 * @return 动画对象
		 * 
		 */		
		public function playCartoon(cartoonStr:String,isStop:Boolean=false,frame:int=1):MovieClip_BMP
		{
			this.reset();
			var cartoon:MovieClip_BMP=this.imgCartoonsObj[cartoonStr] as MovieClip_BMP;
			if(!cartoon)throw new GameError("该动画不存在");
			this.cartoonLayer.addChild(cartoon);
			cartoon.gotoAndPlay(frame);
			cartoon.isStop=isStop;
			this.nowPlayingCartoonStr=cartoonStr;
			return cartoon;
		}
		
		protected function getMyTmpSourceName():String
		{
			return "";
		}
		
		protected function getMyCartoonsStrAry():Array
		{
			return new Array;
		}
		
		/**
		 *复位 
		 * 
		 */		
		public function reset():void
		{
			while(this.cartoonLayer.numChildren)this.cartoonLayer.removeChildAt(0);
		}
		
		/**
		 *释放 
		 * 
		 */		
		public function release():void
		{
			if(this.parent)this.parent.removeChild(this);
			this.removeListener();
			for each(var cartoon:MovieClip_BMP in this.imgCartoonsObj)
			{
				cartoon.release();
			}
			while(this.numChildren)this.removeChildAt(0);
			this._imgCartoonsObj=null;
		}

		/*getter & setter*/
		public function get imgCartoonsObj():Object
		{
			return _imgCartoonsObj;
		}

		public function get nowPlayingCartoonStr():String
		{
			return _nowPlayingCartoonStr;
		}

		public function set nowPlayingCartoonStr(value:String):void
		{
			_nowPlayingCartoonStr = value;
		}

		public function get baseName():String
		{
			return _baseName;
		}

		public function get cartoonLayer():Sprite
		{
			return _cartoonLayer;
		}


	}
}